package com.lxy.common.core.domain;

import com.lxy.common.core.enums.ResultCode;
import lombok.Data;

@Data
public class R<T> {

    private int code; //固定的code

    private String msg;

    private T data;

    public static <T> R<T> ok(){

        return assembleResult(null, ResultCode.SUCCESS);

    }

    public static <T> R<T> ok(T data){

        return assembleResult(data, ResultCode.SUCCESS);

    }

    public static <T> R<T> fail(){
        return assembleResult(null, ResultCode.FAILED);
    }

    //指定错误码
    public static <T> R<T> fail(ResultCode resultCode){
        return assembleResult(null,resultCode);
    }

    private static <T> R<T> assembleResult(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setData(data);
        r.setMsg(resultCode.getMsg());
        return r;
    }


}
